<template>
  <div>
    <el-container>
      <el-header>
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">学科管理</el-breadcrumb-item>
          <el-breadcrumb-item>大数据</el-breadcrumb-item>
          <el-breadcrumb-item>目录管理</el-breadcrumb-item>
        </el-breadcrumb>
      </el-header>
      <el-main>
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form label-width="100px" class="demo-ruleForm">
              <el-form-item label="目录名称">
                <el-input v-model="textplay"></el-input>
              </el-form-item>
            </el-form>
          </el-col>
          <el-col :span="8"><el-button @click="editem"> 清除</el-button><el-button type="primary">搜索</el-button></el-col>
          <el-col :span="8"> <el-button type="success" icon="el-icon-edit" @click="getadd">新增学科</el-button></el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="10" style="padding-right: 20px; font-size: 16px">
            <el-form label-width="80px">
              <el-form-item label="状态">
                <el-select placeholder="请选择" v-model="diectoryData.state">
                  <el-option v-for="item in options" :key="item.index" :label="item.label" :value="item.state"> </el-option>
                </el-select>
              </el-form-item>
            </el-form>
          </el-col>
        </el-row>
        <el-row style="margin: 20px 0 10px 0">
          <el-alert title="消息提示的文案" type="info" show-icon> </el-alert>
        </el-row>
        <el-row>
          <el-table :data="diectoryData.items" stripe style="width: 100%">
            <el-table-column type="index" label="序号"> </el-table-column>
            <el-table-column prop="subjectName" label="所属学科"> </el-table-column>
            <el-table-column prop="directoryName" label="目录名称"> </el-table-column>
            <el-table-column prop="creator" label="创建者"> </el-table-column>
            <el-table-column prop="addDate" label="创建日期"> </el-table-column>
            <el-table-column prop="totals" label="面试数量"> </el-table-column>
            <el-table-column label="状态">
              <template slot-scope="scope">
                <p v-if="scope.row.state === 1">已启用</p>
                <p v-else>已禁用</p>
              </template>
            </el-table-column>
            <el-table-column prop="date" label="操作" width="155">
              <template slot-scope="scope">
                <el-button type="text" v-if="scope.row.state === 1" @click="turnBtn(scope.row.state, scope.row.id)" style="margin-right: 10px; font-size: 12px; color: #409eff">禁用</el-button>
                <el-button type="text" v-else @click="turnBtn(scope.row.state, scope.row.id)" style="margin-right: 10px; font-size: 12px; color: #409eff">启用</el-button>
                <el-button type="text" @click="modifyDirectory(scope.row.id)" style="margin-right: 10px; font-size: 12px; color: #409eff">修改</el-button>
                <el-button type="text" @click="deleteData(scope.row.id)" style="margin-right: 10px; font-size: 12px; color: #409eff">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-row>
      </el-main>
    </el-container>
    <el-dialog title="新增学科" :visible.sync="addDite" width="20%">
      <el-form label-width="80px" :model="subgect" ref="addTagsFormRef">
        <el-form-item label="所属学科">
          <el-select v-model="subgect.subjectID" placeholder="请选择">
            <el-option v-for="(item, index) in subgect.items" :key="index" :label="item.subjectName" :value="item.id"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称" prop="Directory">
          <el-input v-model="subgect.directoryName"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDite = false">取 消</el-button>
        <el-button type="primary" @click="modeladd">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="修改目录" :visible.sync="dialogVisible" width="20%">
      <el-form label-width="80px" v-model="editform" ref="reference">
        <el-form-item label="所属学科">
          <el-select v-model="editform.subjectID" placeholder="请选择">
            <el-option v-for="(item, index) in diectoryData.items" :key="index" :label="item.subjectName" :value="item.id"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称" prop="Directory">
          <el-input v-model="editform.directoryName"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editformits">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'directorys',
  data () {
    return {
      values: [],
      editform: {
        subjectID: '',
        directoryName: ''
      },
      textplay: '',
      subgect: {
        directoryName: '',
        subjectID: ''
      },
      options: [
        { state: 1, label: '已启用' },
        { state: 0, label: '已屏蔽' }
      ],
      addDite: false,
      diectoryData: [],
      dialogVisible: false,
      dataes: '',
      playes: ''
      // addFon: {
      //   Directory: [
      //     { required: true, message: '请输入目录名称', trigger: 'blur' }
      //   ]
      // }
    }
  },
  created () {
    this.getlist()
  },
  methods: {
    editem () {
      this.textplay = ''
    },
    async getlist () {
      // const res = await this.$http.get('/directorys')
      const { data: res } = await this.$http.get('/directorys')
      this.diectoryData = res
      console.log(this.diectoryData)
    },
    async handleChange () {
      const res = await this.$http.post('/directorys/:id/:state')
      console.log(res)
    },
    // 删除方法
    deleteData (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await this.$http.delete(`/directorys/${id}`)
        console.log(res)
        this.$message.success('删除成功')
        this.getlist()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 修改目录
    modifyDirectory (row) {
      console.log(row)
      this.dataes = row
      this.dialogVisible = true
    },
    async editformits () {
      const res = await this.$http.put(`/directorys/${this.dataes}`, this.editform)
      if (res.status !== 200) return this.$message.error('获取数据失败')
      this.$message.success('获取数据成功')
    },
    // async Enable (row) {
    //   const res = await this.$http.post(`/directorys/${row.id}/${row.state}`)
    //   console.log(res)
    //   this.getlist()
    //   this.$message.success('更新数据成功')
    // }
    async getadd () {
      this.addDite = true
      const { data: res } = await this.$http.get('/directorys')
      this.subgect = res
    },
    async turnBtn (state, id) {
      var newState = state === 1 ? 0 : 1
      const { data: res } = await this.$http.post(`/directorys/${id}/${newState}`)
      if (res.success !== true) return this.$message.error('变更状态失败！')
      this.$message.success('变更状态成功！')
      this.getlist()
    },
    async modeladd () {
      this.$refs.addTagsFormRef.validate(async valid => {
        if (!valid) return
        await this.$http.post('/directorys', this.subgect)
        // if (res.success !== true) return this.$message.error('添加标签失败！')
        // console.log(res)
        this.$message.success('添加标签成功！')
        this.addDite = false
        this.getlist()
      })
    }
  }
}
</script>

<style scoped lang="less">
.el-container {
  background-color: #fff;
}
</style>
